// combo_checker.cpp
#include "combo_checker.h"
#include <QMessageBox>
#include <QDebug>
#include <QPixmap>
#include <QGraphicsScene>

ComboChecker::ComboChecker(QObject *parent) : QObject(parent) {}

bool ComboChecker::checkAndShowCombo(
    DragablePixmapItem *upItem, 
    DragablePixmapItem *downItem,
    DragablePixmapItem *dressItem, 
    DragablePixmapItem *shoesItem,
    const QList<OutfitCombo> &combos,
    const std::function<ClothesInfo(DragablePixmapItem*)> &getInfoFunc,
    QGraphicsScene *scene, 
    QGraphicsPixmapItem *&comboItem,
    QString &currentComboPath, 
    bool &showingCombo,
    QWidget *parentWidget,
    QGraphicsPixmapItem *characterItem,
    const QSize &windowSize)
{

    // 检查是否有完整搭配
    bool hasCompleteOutfit = (
        (upItem && upItem->isWorn() && downItem && downItem->isWorn() && shoesItem && shoesItem->isWorn()) ||
        (dressItem && dressItem->isWorn() && shoesItem && shoesItem->isWorn())
    );

    if (!hasCompleteOutfit)
    {
        QMessageBox::information(parentWidget, "提示", "请搭配完整套装（上衣+下装+鞋子 或 连衣裙+鞋子）");
        return false;
    }

    // 获取当前穿戴的服饰名称
    QString topName = (upItem && upItem->isWorn()) ? getInfoFunc(upItem).name : "";
    QString dressName = (dressItem && dressItem->isWorn()) ? getInfoFunc(dressItem).name : "";
    QString bottomName = (downItem && downItem->isWorn()) ? getInfoFunc(downItem).name : "";
    QString shoesName = (shoesItem && shoesItem->isWorn()) ? getInfoFunc(shoesItem).name : "";

    qDebug() << "当前搭配 - 上衣:" << topName << "连衣裙:" << dressName << "下装:" << bottomName << "鞋子:" << shoesName;

    // 遍历套装查找匹配
    for (const auto &combo : combos)
    {
        qDebug() << "检查套装:" << combo.name << "要求 - 上衣:" << combo.top
                 << "连衣裙:" << combo.dress << "下装:" << combo.bottom << "鞋子:" << combo.shoes;

        // 匹配上衣+下装+鞋子
        if (!combo.top.isEmpty() && combo.dress.isEmpty())
        {
            if (combo.top == topName && combo.bottom == bottomName && combo.shoes == shoesName)
        {
            // 隐藏原来的角色和衣服
            if (characterItem) characterItem->setVisible(false);
            if (upItem) upItem->setVisible(false);
            if (downItem) downItem->setVisible(false);
            if (dressItem) dressItem->setVisible(false);
            if (shoesItem) shoesItem->setVisible(false);
            
            // 显示合成图
            hideCombo(scene, comboItem, currentComboPath, showingCombo);
            comboItem = new QGraphicsPixmapItem;
            QPixmap pixmap(combo.comboImagePath);
            if (pixmap.isNull()) {
                delete comboItem;
                comboItem = nullptr;
                return false;
            }
            comboItem->setPixmap(pixmap);
            scene->addItem(comboItem);
            currentComboPath = combo.comboImagePath;
            showingCombo = true;
            
            // 立即缩放合成图以适应窗口
            scaleAndPositionCombo(comboItem, combo.comboImagePath, windowSize, nullptr);

            QMessageBox::information(parentWidget, "套装匹配成功",
                                    QString("恭喜！已匹配到「%1」套装").arg(combo.name));
            return true;
        }
        }
        // 匹配连衣裙+鞋子
        else if (combo.top.isEmpty() && !combo.dress.isEmpty())
        {
            if (combo.dress == dressName && combo.shoes == shoesName)
            {
                // 隐藏原来的角色和衣服
                if (characterItem) characterItem->setVisible(false);
                if (upItem) upItem->setVisible(false);
                if (downItem) downItem->setVisible(false);
                if (dressItem) dressItem->setVisible(false);
                if (shoesItem) shoesItem->setVisible(false);
                
                // 显示合成图
                hideCombo(scene, comboItem, currentComboPath, showingCombo);
                comboItem = new QGraphicsPixmapItem;
                QPixmap pixmap(combo.comboImagePath);
                if (pixmap.isNull())
                {
                    delete comboItem;
                    comboItem = nullptr;
                    return false;
                }
                comboItem->setPixmap(pixmap);
                scene->addItem(comboItem);
                currentComboPath = combo.comboImagePath;
                showingCombo = true;
                
                // 立即缩放合成图以适应窗口
                scaleAndPositionCombo(comboItem, combo.comboImagePath, windowSize, nullptr);

                QMessageBox::information(parentWidget, "套装匹配成功",
                                        QString("恭喜！已匹配到「%1」套装").arg(combo.name));
                return true;
            }
        }
    }

    QMessageBox::information(parentWidget, "未匹配到套装", "当前搭配未组成任何套装");
    return false;
}

void ComboChecker::hideCombo(QGraphicsScene *scene, QGraphicsPixmapItem *&comboItem,
                           QString &currentComboPath, bool &showingCombo)
{
    if (comboItem)
    {
        scene->removeItem(comboItem);
        delete comboItem;
        comboItem = nullptr;
    }
    currentComboPath.clear();
    showingCombo = false;
}

void ComboChecker::scaleAndPositionCombo(QGraphicsPixmapItem *comboItem, const QString &comboPath,
                                       const QSize &windowSize, QGraphicsView *view)
{
    if (!comboItem || comboPath.isEmpty()) return;
    
    QPixmap pixmap(comboPath);
    if (pixmap.isNull()) return;
    
    // 计算缩放因子（使用与ScaleManager相同的逻辑）
    qreal scaleFactor = qMin(windowSize.width() / 1000.0, windowSize.height() / 700.0);
    scaleFactor = qBound(0.5, scaleFactor, 2.0); // 限制缩放范围
    
    // 设置缩放后的图像
    QSize scaledSize = pixmap.size() * scaleFactor * 0.26;
    comboItem->setPixmap(pixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    
    // 居中定位
    if (view)
    {
        // 如果提供了view，计算view中心的场景坐标
        QRectF viewRect = view->mapToScene(view->viewport()->rect()).boundingRect();
        QPointF centerPos(
            viewRect.center().x() - comboItem->boundingRect().width() / 2,
            viewRect.center().y() - comboItem->boundingRect().height() / 2 + 180
        );
        comboItem->setPos(centerPos);
    }
    else
    {
        // 如果没有view，设置在窗口中央的默认位置
        comboItem->setPos(200, 150); // 默认位置
    }
}
